package com.example.yml0601;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yml.R;

public class Main4Activity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_sj;
    private TextView tv_sj;
    private Button button;
    private MyAsyncTask myAsyncTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        initView();
    }

    private void initView() {
        et_sj = (EditText) findViewById(R.id.et_sj);
        tv_sj = (TextView) findViewById(R.id.tv_sj);
        button = (Button) findViewById(R.id.button);

        et_sj.addTextChangedListener(textWatcher);//给edittext添加监听器
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                String trim = et_sj.getText().toString().trim();
                int intent = Integer.parseInt(trim);
                myAsyncTask = new MyAsyncTask();
                myAsyncTask.execute(intent);

                break;
        }
    }

    @Override
    protected void onPause() {
        //myAsyncTask.getStatus() == AsyncTask.Status.RUNNING  判断当前是否是运行状态
        if (myAsyncTask != null && myAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
            myAsyncTask.cancel(true);
        }
        super.onPause();
    }

    private class MyAsyncTask extends AsyncTask<Integer, Integer, Void> {
        @Override
        protected Void doInBackground(Integer... integers) {
            Integer integer = integers[0];
            for (int i = 0; i <= integer; ) {
                for (int j = 0; j < 60; j++) {
                   /* if (j==59){
                        i++;
                    }*/
                    if (i == integer) {
                        return null;
                    }
                    publishProgress(j, i);
                    SystemClock.sleep(100);
                }

            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            Integer value = values[0];
            Integer value1 = values[1];
            String data = null;
           /* if (value==59){
                value=0;
            }*/
            if (value < 10) {
                data = "0" + value;
            } else {
                data = value + "";
            }
            if (value1 < 10) {
                data = "0" + value1 + " : " + data;
            } else {
                data = value1 + ":" + data;
            }

            tv_sj.setText(data);
        }

        @Override
        protected void onCancelled() {

            super.onCancelled();
        }
    }

    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String contet = et_sj.getText().toString().trim();
            if (contet != null && contet.length() > 0) {
                button.setEnabled(true);
            } else {
                button.setEnabled(false);
            }
        }
    };

}

